
//
//  RootViewController.swift
//  Swift微博
//
//  Created by 陈竹 on 16/2/22.
//  Copyright © 2016年 陈竹. All rights reserved.
//

import UIKit
import HMRefresh


class RootViewController: UIViewController {
    //MARK:- 公共属性
    
    ///用户登陆标记
    var userLogin = false
    ///表视图
    //    lazy var tableView=UITableView(frame: CGRectZero, style: .Plain)
    var tableView:UITableView?
    ///下拉刷新控件
    var refreshControl:HMRefreshControl?
    
    ///上拉刷新视图
    lazy var pullUpView=UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    
    var visitorView:VisitorView?
    
    
    override func loadView() {
        view=UIView()
        //根据用户touken判断是否现实访客视图
        UserAccount.sharedUserAccount.isLogin ? setupTableView() : setupVistorView()
        
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if UserAccount.sharedUserAccount.isLogin{
            loadData()
        }else{
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "loginSuccess", name: CZWeiBoLoginSuccessedNotification, object: nil)
        }
        
        
        
    }
    func loadData(){
        
    }
    
    @objc private func loginSuccess(){
        view=nil
        
        NSNotificationCenter.defaultCenter().removeObserver(self, name: CZWeiBoLoginSuccessedNotification, object: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}

// MARK: - 访客视图的登陆注册
extension RootViewController:VisitorViewDelegate{
    
    func VisitorDidLogin() {
        
        let auth=OAuthViewController()
        let nav=UINavigationController(rootViewController: auth)
        presentViewController(nav, animated: true, completion: nil)
        print("登陆")
    }
    func VisitorDidRegister() {
        print("注册")
    }
    
}

extension RootViewController{
    ///设置访客视图
    private func setupVistorView(){
        visitorView=VisitorView()
        view.addSubview(visitorView!)
        //自动布局，访客视图上下左右等于当前view的上下左右
        visitorView?.snp_makeConstraints(closure: { (make) -> Void in
            make.edges.equalTo(view)
        })
        visitorView?.delegate=self
    }
    ///设置表格视图
    private func setupTableView(){
         tableView=UITableView(frame: CGRectZero, style: .Plain)
        view=tableView
        //设置数据源，代理
        tableView?.delegate=self
        tableView?.dataSource=self
        
    }
    //设置下拉刷新
    func setupRefreshControl(){
        guard let tableView=tableView else{
        return
        }
        if refreshControl != nil{
        return
        }
        refreshControl=HMRefreshControl()
        
        tableView.addSubview(refreshControl!)
        
        //设置下拉刷新属性
        refreshControl?.pulldownView=CZPulldownRefreshView.refreshView()
        refreshControl?.normalString = "下拉起飞"
        refreshControl?.pullingString = "放开起飞"
        refreshControl?.refreshingString = "正在起飞"
        refreshControl?.lastRefreshString = "末次 "
        //添加下拉刷新table底部视图
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
    }
    
    
}


extension RootViewController:UITableViewDelegate,UITableViewDataSource{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    
    
}


